﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CC.Common
{
    /// <summary>
    /// 数据列属性
    /// 用于修饰自定义类，指定要忽略的财产(Property), 财产的别名（）以及ID列特性等
    /// </summary>
    public class DataColumnAttribute : Attribute
    {
        public bool ColumnIgnore { get; set; }
        public string ColumnName { get; set; }
        /// <summary>
        /// 是否为ID列。
        /// </summary>
        public bool IdColumn { get; set; }
        /// <summary>
        /// 是否为自增列，包括自动GUID
        /// </summary>
        public bool AutoIncrement { get; set; }

        public DataColumnAttribute()
        {
            ColumnIgnore = false;
            AutoIncrement = false;
        }
        #region 读取DataColumnAttribute属性
        /// <summary>
        /// 利用DataColumnAttribute属性，判断是否要忽略该属性
        /// </summary>
        /// <param name="type">要判断的类，如Student</param>
        /// <param name="propName">类的属性,如Name</param>
        /// <returns></returns>
        public static bool IsColumnIgnore(PropertyInfo pi)
        {
            DataColumnAttribute[] attributes = (DataColumnAttribute[])pi.GetCustomAttributes(typeof(DataColumnAttribute), false);
            bool isIgnore = false;
            if (attributes != null && attributes.Length > 0)
                isIgnore = attributes[0].ColumnIgnore;
            return isIgnore;
        }

        /// <summary>
        /// 利用DataColumnAttribute属性，判断是否为自增列，包括自动GUID
        /// </summary>
        /// <param name="type">要判断的类，如Student</param>
        /// <param name="propName">类的属性,如Name</param>
        /// <returns></returns>
        public static bool IsAutoIncrement(PropertyInfo pi)
        {
            DataColumnAttribute[] attributes = (DataColumnAttribute[])pi.GetCustomAttributes(typeof(DataColumnAttribute), false);
            bool isId = false;
            if (attributes != null && attributes.Length > 0)
                isId = attributes[0].AutoIncrement;
            return isId;
        }
        /// <summary>
        /// 利用DataColumnAttribute属性，判断是否为自增列，包括自动GUID
        /// </summary>
        /// <param name="type">要判断的类，如Student</param>
        /// <param name="propName">类的属性,如Name</param>
        /// <returns></returns>
        public static bool IsIdColumn(PropertyInfo pi)
        {
            DataColumnAttribute[] attributes = (DataColumnAttribute[])pi.GetCustomAttributes(typeof(DataColumnAttribute), false);
            bool isId = false;
            if (attributes != null && attributes.Length > 0)
                isId = attributes[0].IdColumn;
            return isId;
        }
        /// <summary>
        /// 获取利用DataColumnAttribute属性设定的列名
        /// 如果没有设置列名，则用类财产名
        /// </summary>
        /// <param name="type">类</param>
        /// <param name="propName">类属性</param>
        /// <returns>如果设置了列名，返回列名，否则返回类财产名</returns>
        public static string GetColumnName(PropertyInfo pi)
        {

            DataColumnAttribute[] attributes = (DataColumnAttribute[])pi.GetCustomAttributes(typeof(DataColumnAttribute), false);
            string name = pi.Name;
            if (attributes != null && attributes.Length > 0)
                name = attributes[0].ColumnName;
            return name;
        }
        /// <summary>
        /// 通过列名寻找PropertyInfo
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="colName"></param>
        /// <returns>返回类属性中名称或者用DataColumnAttribute定义的列名为colName的类属性</returns>
        public static PropertyInfo GetPropertyInfo<T>(string colName)
        {
            Type type = typeof(T);
            var props = type.GetProperties();
            return props.FirstOrDefault(p => GetColumnName(p) == colName || p.Name == colName);
        }
        #endregion
    }
}
